const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');
var app = getApp();

Page({
  data: {
    userName: '',
    password: ''
  },

  onLoad: function (options) {

  },
  onShow: function () {
    // this.login()
  },

  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },

  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  loginTap: function (e) {
    if(this.data.userName == ''){
      wx.showToast({
        title: '请输入用户名！',
      })
      return
    }

    if(this.data.userName == ''){
      wx.showToast({
        title: '请输入密码！',
      })
      return
    }
    this.login()
  },

  toLineDetails: function (e) {
    console.log(e.currentTarget.dataset.a)
    if (e.currentTarget.dataset.b == null || e.currentTarget.dataset.a == null) {
      wx.showToast({
        title: '参数异常',
      })
      return
    }
    wx.navigateTo({
      url: '../lineDetails/lineDetails?routeId=' + e.currentTarget.dataset.a + '&direction=' + e.currentTarget.dataset.b + '&lineName=' + e.currentTarget.dataset.c,
    })
  },

  //登录
  login: function (e) {
    let data = {
      username: this.data.userName,
      password: this.data.password
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.loginUrl,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          wx.setStorageSync('id', res.data.id)
          wx.setStorageSync('imgUrl', res.data.imgUrl)
          wx.setStorageSync('mobileNumber', res.data.mobileNumber)
          wx.setStorageSync('password', res.data.password)
          wx.setStorageSync('userType', res.data.userType)
          wx.setStorageSync('username', res.data.username)
          wx.setStorageSync('workUnitID', res.data.workUnitID)
          wx.setStorageSync('workUnitName', res.data.workUnitName)
          wx.hideLoading()
          wx.navigateBack({
            complete: (res) => {},
          })
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },
})